/*
 ============================================================================
 Name        : dc_analysis.c
 Author      : shaomin.shi
 Version     : 1.0
 Copyright   : HappyElements
 Description : dc_analysis TOOL
 Usage       : dc_analysis -c d/m/h -f 201202019.dat
				-c d -f log_filename
				d --day:
				h --hour
				m --minute
============================================================================
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dc_analysis.h"
#include "crc32.h"

int main( int argc , char *argv[] )
{
	if(argc!=5){
		printf("usage : dc_analysis -c d/h/m -f filename.dat\n");
		return EXIT_FAILURE;
	}
	if(strcmp(argv[1],"-c")!=0){
		printf("parameter -c error!\n");
		return EXIT_FAILURE;
	}
	if(strcmp(argv[3],"-f")!=0){
		printf("parameter -f error!\n");
		return EXIT_FAILURE;
	}
	char type = argv[2][0];
	char *filename = argv[4];
	if(type!=SELECT_DAY && type!=SELECT_HOUR && type!=SELECT_MINUTE){
		printf("parameter -c value error!\n");
		return EXIT_FAILURE;
	}
//	char type = 'd';
//	char *filename = "c:\\dc.log";
	FILE *fp;
	if((fp=fopen(filename,"r"))==NULL)
	{
		printf("can not open file: %s\n" , filename);
		return EXIT_FAILURE;
	}
	char *line = (char *)malloc( MAX_LINE_SIZE );
	while(!feof(fp))
	{
		fgets(line, MAX_LINE_SIZE, fp);
		parse_log_line(line);
		int  /*hour=0,min=0,minute=0,*/ game_index =0;
		//char str[30];
		switch(type)
		{
			case SELECT_DAY:
				game_index = get_game_index(log_struct.game_name);
				log_result[game_index].step[log_struct.step]++;
				log_result[game_index].num++;
				break;
			case SELECT_HOUR:
				/*
				substring( str , log_struct.log_time ,8 , 2 );
				hour = atoi(str);
				log_result[hour].cycle = (char *)malloc(12);
				substring( str , log_struct.log_time ,0 ,8 );
				sprintf(log_result[hour].cycle,"%s%02d",str,hour);
				log_result[hour].step[log_struct.step]++;
				log_result[hour].num++;
				*/
				break;
			case SELECT_MINUTE:
				/*
				substring( str , log_struct.log_time ,8 , 2 );
				hour = atoi(str);
				substring( str , log_struct.log_time ,10, 2 );
				min = atoi(str);
				minute = (hour*60)+min;
				if(minute>=_MINUTE) continue;
				log_result[minute].cycle = (char *)malloc( 20 );
				substring( str , log_struct.log_time ,0 ,8 );
				sprintf(log_result[minute].cycle,"%s%02d%02d",str,hour,min);
				log_result[minute].step[log_struct.step]++;
				log_result[minute].num++;
				*/
				break;
		}
	}

	/*
	 * 输出一天数据
	 */
	if(type==SELECT_DAY){
		int i,j;
		printf("step\tcount\tratio\n");
		for(i=0;i<MAX_GAME_NUM;i++)
		{
			if(game_data[i].game_index==0) continue;
			printf("%s:\n",game_data[i].game_name);
			for( j=0;j<MAX_STEP;j++ )
			{
				if(log_result[i].step[j]!=0)
				{	//以20步为基准
					if(log_result[i].step[20]>0)
					{
						float rate =(float)log_result[i].step[j]/log_result[i].step[20];
						printf("%d\t%u\t%2.4f\n",j,log_result[i].step[j],rate);
					}else{
						printf("%d\t%u\t%2.4f\n",j,log_result[i].step[j],1.0000f);
					}
				}
			}
			free(game_data[i].game_name);
		}
	}
	/*
	 * 输出每小时数据
	 */
	/*
	if(type==SELECT_HOUR){
		int i,j;
		printf("step\tcount\tratio\n");
		for(i=0;i<_HOUR;i++)
		{
			if(log_result[i].num<=0) continue;
			printf("%s:\n" , log_result[i].cycle);
			for( j=0;j<MAX_STEP;j++ )
			{
				if(log_result[i].step[j]!=0)
				{
					float rate =(float)log_result[i].step[j]/log_result[i].num;
					printf("%d\t%u\t%f\n",j,log_result[i].step[j], rate);
				}
			}
			free(log_result[i].cycle); //free memory
		}
	}
	*/
	/*
	 * 输出每分钟数据
	 */
	/*
	if(type==SELECT_MINUTE){
		int i,j;
		printf("step\tcount\tratio\n");
		for(i=0;i<_MINUTE;i++)
		{
			if(log_result[i].num<=0) continue;
			printf("%s:\n" , log_result[i].cycle);
			for( j=0;j<MAX_STEP;j++ )
			{
				if(log_result[i].step[j]!=0)
				{
					float rate =(float)log_result[i].step[j]/log_result[i].num;
					printf("%d\t%u\t%2.4f\n",j,log_result[i].step[j], rate);
				}
			}
			free(log_result[i].cycle); //free memory
		}
	}
	 */
	fclose(fp);
	free(line);
	free(log_struct.log_time);
	free(log_struct.game_name);
	free(log_struct.gip_user_id);
	return EXIT_SUCCESS;
}

/*
 * 将每行的数据解析到结构体
 */
void parse_log_line(char *str_line){
	int i,len=0,index = 0;
	char str[MAX_SECTION_SIZE];
	for(i=0; i<strlen(str_line); i++)
	{
		if(*(str_line+i) == '\n') continue;
		str[len++] = *(str_line+i);
		if(*(str_line+i)=='\t')
		{
			str[--len] = '\0';
			switch(index)
			{
				case LOG_TIME_INDEX:
					log_struct.log_time = (char *)malloc( len+1 );
					strcpy(log_struct.log_time , str);
					break;
				case PLATFORM_INDEX:
					log_struct.platform = atoi(str);
					break;
				case GAME_NAME_INDEX:
					log_struct.game_name = (char *)malloc( len+1 );
					strcpy( log_struct.game_name , str);
					break;
				case SETP_INDEX:
					log_struct.step = atoi(str);
					break;
				case GIP_USER_ID_INDEX:
					log_struct.gip_user_id = (char *)malloc( len +1);
					strcpy( log_struct.gip_user_id , str);
					break;
				case IP_INDEX:
					break;
				case INTERVAL_INDEX:
					break;
			}
			index++;
			if(index>=IP_INDEX){
				break;
			}
			len = 0;
		}
	}
}

/*
 * 截取字符串
 */
char *substring(char *dst,char *src, int start, int len)
{
	char *p=dst;
	char *q=src;
	int length=strlen(src);
	if(start>=length||start<0) return NULL;
	if(len>length) len=length-start;
	q+=start;
	while(len--)
	{
	    *(p++)=*(q++);
	}
	*(p++)='\0';
	return dst;
}

int get_game_index(char *game_name)
{
	unsigned int crc = GetCrc32( game_name, (unsigned int)strlen(game_name) );
	int i;
	for(i=0;i<MAX_GAME_NUM;i++)
	{
		if(game_data[i].game_index==crc)
		{
			return i;
		}
		if(game_data[i].game_index == 0)
		{
			game_data[i].game_index = crc;
			game_data[i].game_name = (char *)malloc(MAX_SECTION_SIZE);
			strcpy(game_data[i].game_name, game_name);
			return i;
		}
	}
	return 0;
}


